package com.welike.shibo.controller.v2.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.welike.shibo.aspect.annotation.AutoLog;
import com.welike.shibo.eneity.Audio;
import com.welike.shibo.eneity.JsonResult;
import com.welike.shibo.service.AudioService;
import com.welike.shibo.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fangyu
 * @version v1.0.0
 * @since 2019/11/20 5:33 下午
 */
@Slf4j
@Api(value = "音频相关接口")
@RestController
@RequestMapping("/v2/api/music")
public class MusicController {

    @Resource
    private AudioService audioService;

    @AutoLog(operateState = "音乐列表")
    @RequiresPermissions("music:list")
    @GetMapping("/list")
    public JsonResult list(
            @ApiParam(name = "page", value = "页数", required = false)
            @RequestParam(value = "page", defaultValue = "1", required = false) int page,
            @ApiParam(name = "pageSize", value = "每页条数", required = false)
            @RequestParam(value = "pageSize", defaultValue = "10", required = false)int pageSize
    ){
        IPage<Audio> audioList = audioService.queryAllAudio(page, pageSize);
//        List<Audio> audios = new ArrayList<>();
//        for (Audio audio: audioList.getRecords()){
//            audio.setAudioUrl("https://cdn.health.healthplatform.xyz/"+audio.getAudioUrl());
//            audios.add(audio);
//        }
        return ResultUtils.success("list", audioList);
    }

    /**
     * 上传音乐
     * @param music 音乐
     * @return JsonResult
     * @throws IOException IO异常
     */
    @AutoLog(operateState = "上传音乐")
    @RequiresPermissions("music:add")
    @PostMapping("/upload")
    public JsonResult upload(@RequestParam("file") MultipartFile music) throws IOException {
        String musicUrl = audioService.uploadFile(music);
        Map<String, Object> result = new HashMap<>();
        result.put("music", musicUrl);
        return ResultUtils.success(result);
    }

    /**
     * 添加音乐
     * @param requestJson 请求json
     * @return JsonResult
     */
    @AutoLog(operateState = "添加音乐")
    @RequiresPermissions("music:add")
    @PostMapping("/add")
    public JsonResult addMusic(@RequestBody JSONObject requestJson){
        return audioService.addMusic(requestJson);
    }

    /**
     * 删除音乐
     * @param id 音乐id
     * @return JsonResult
     */
    @AutoLog(operateState = "删除音乐")
    @RequiresPermissions("music:delete")
    @PostMapping("/delete")
    public JsonResult deleteMusic(@RequestParam String id){
        return audioService.deleteMusicById(id);
    }

    @AutoLog(operateState = "改变音乐状态")
    @RequiresPermissions("member:update")
    @PostMapping(value = "/status")
    public JsonResult updateStatus(@RequestParam String id, @RequestParam String status){
        return audioService.updateStatus(id, status);
    }
}
